package com.dofun.locationservice.base;

import android.content.Context;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.dofun.locationservice.core.MyApplication;
import com.dofun.locationservice.manager.ActivityStackManager;
import com.dofun.locationservice.util.L;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * Created by G on 2018/8/6.
 */

public abstract class BaseActivity extends BaseAppCompatActivity
        implements EasyPermissions.PermissionCallbacks {

    protected boolean isNeedBackKey = false;
    protected boolean isNeedLoginout = true;

    private MyApplication myApplication;

    protected Context mContext;
    protected Unbinder unBinder;

    protected LinearLayout mainBody;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStackManager.getManager().push(this);
        setContentView(getContentViewId());
        mContext = this;
        unBinder = ButterKnife.bind(this);
        initBundleData();
        initView(savedInstanceState);
        if (isNeedBackKey)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initData();
    }

    @Override
    public void setContentView(int layoutResID) {
        if(mainBody != null){
            mainBody.removeAllViews();
            mainBody.addView(this.getLayoutInflater().inflate(layoutResID, null)
                    , new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT
                            ,WindowManager.LayoutParams.MATCH_PARENT));
        } else {
            super.setContentView(layoutResID);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //移除view绑定
        if (unBinder != null) {
            unBinder.unbind();
        }
        ActivityStackManager.getManager().remove(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> list) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> list) {
    }

    @Override
    public void showErrorMsg(String msg) {
        //请求数据返回错误信息，默认日志打印
        L.e(msg);
    }

    /**
     * 获取显示view的xml文件ID
     */
    protected abstract int getContentViewId();


    /**
     * 获取上一个界面传送过来的数据
     */
    protected abstract void initBundleData();

    /**
     * 初始化view
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 初始化Data
     */
    protected abstract void initData();

    /**
     * @author Elvis
     * @time 2017/5/11 12:54
     * @description 获取状态栏高度，当状态栏全屏时, 会超出屏幕, 给其上部设置padding
     */
    public int getStatusBarHeight() {
        int statusBarHeight = 0;
//        int resourceId = getResources().getIdentifier("app_bar_height", "dimen", "android");  //这里设置状态栏失败 - -！
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBack();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void onBack() {}
}
